package it.rockliveitalia;

import java.util.Calendar;

import java.util.concurrent.ExecutionException;

import com.google.analytics.tracking.android.EasyTracker;

import it.rockliveitalia.entity.Event;
import it.rockliveitalia.feedback.FeedbackHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class InputFormActivity extends Activity {

	private EditText eventVenue = null;
	private EditText eventAddressStreet = null;
	private EditText eventAddressCity = null;
	private EditText eventAddressDistrict = null;
	private EditText eventVenueSite = null;
	private EditText eventBand = null;
	private EditText eventBandSite = null;
	private EditText eventDescription = null;
	private EditText eventDate = null;
	private String eventDateString = null;
	private Context context;

	private Event e = new Event();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_input_form);
		context = this.getApplicationContext();

		eventVenue = (EditText) findViewById(R.id.event_venue);
		eventAddressStreet = (EditText) findViewById(R.id.event_address_street);
		eventAddressCity = (EditText) findViewById(R.id.event_address_city);
		eventAddressDistrict = (EditText) findViewById(R.id.event_address_district);
		eventVenueSite = (EditText) findViewById(R.id.event_venue_site);
		eventBand = (EditText) findViewById(R.id.event_band);
		eventBandSite = (EditText) findViewById(R.id.event_band_site);
		eventDate = (EditText) findViewById(R.id.event_date);
		eventDescription = (EditText) findViewById(R.id.event_description);
	}

	public void checkEditText(View view) throws InterruptedException,
			ExecutionException {

		if (!eventVenue.getText().toString().equals("")
				&& !eventAddressStreet.getText().toString().equals("")
				&& !eventAddressCity.getText().toString().equals("")
				&& !eventAddressDistrict.getText().toString().equals("")
				&& !eventBand.getText().toString().equals("")
				&& eventDateString != null) {
			Event event = new Event();
			event.setDate(eventDateString);
			Calendar cal1 = Calendar.getInstance();
			cal1.set(Calendar.HOUR_OF_DAY, 0);
			cal1.set(Calendar.MINUTE, 1);
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(event.getDate());
			if (cal2.getTimeInMillis() > cal1.getTimeInMillis()) {
				createReport();
			} else {
				Toast.makeText(this, R.string.out_of_date, Toast.LENGTH_SHORT)
						.show();
			}
		} else {
			Toast.makeText(this, R.string.field_missing, Toast.LENGTH_SHORT)
					.show();
		}
	}

	private void createReport() {
		String newEvent = null;
		Calendar cal = Calendar.getInstance();
		e.setDate(cal.getTime());
		e.setId(e.getStringDate() + "000");
		e.setVenueName(eventVenue.getText().toString());
		e.setStreet(eventAddressStreet.getText().toString());
		e.setCity(eventAddressCity.getText().toString());
		e.setProvincia(eventAddressDistrict.getText().toString());
		e.setBand(eventBand.getText().toString());
		e.setDate(eventDateString);

		if (!eventVenueSite.getText().toString().equals("")) {
			e.setVenueSite(eventVenueSite.getText().toString());
		} else {
			e.setVenueSite("EMPTY");
		}
		if (!eventBandSite.getText().toString().equals("")) {
			e.setBandSite(eventBandSite.getText().toString());
		} else {
			e.setBandSite("EMPTY");
		}
		if (!eventDescription.getText().toString().equals("")) {
			e.setDescription(eventDescription.getText().toString());
		} else {
			e.setDescription("EMPTY");
		}
		e.setTag("b");
		e.setLat("LAT");
		e.setLng("LNG");

		newEvent = "<Event id=\"" + e.getId() + "\"><venueName>"
				+ e.getVenueName() + "</venueName><venueSite>"
				+ e.getVenueSite() + "</venueSite><date>" + e.getStringDate()
				+ "</date><street>" + e.getStreet() + "</street><city>"
				+ e.getCity() + "</city><provincia>" + e.getProvincia()
				+ "</provincia><band>" + e.getBand() + "</band><bandSite>"
				+ e.getBandSite() + "</bandSite><description>"
				+ e.getDescription() + "</description><image>" + e.getImage()
				+ "</image><tag>" + e.getTag() + "</tag><lat>" + e.getLat()
				+ "</lat><lng>" + e.getLng() + "</lng></Event>";

		FeedbackHelper.newEventFeedback(newEvent);

		AlertDialog.Builder alertb = new AlertDialog.Builder(this);
		alertb.setTitle(R.string.event_sent_title);
		alertb.setMessage(R.string.event_sent_message);
		alertb.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// User clicked OK button
						Intent intent = new Intent(context, MapActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
					}
				});
		alertb.setIcon(R.drawable.ic_navigation_accept);
		AlertDialog alert = alertb.create();
		alert.show();

		return;
	}

	public void showDatePicker(View v) {
		// TODO Auto-generated method stub
		// To show current date in the datepicker
		Calendar mcurrentDate = Calendar.getInstance();
		int mYear = mcurrentDate.get(Calendar.YEAR);
		int mMonth = mcurrentDate.get(Calendar.MONTH);
		int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

		DatePickerDialog mDatePicker = new DatePickerDialog(
				InputFormActivity.this, new OnDateSetListener() {
					public void onDateSet(DatePicker datepicker,
							int selectedyear, int selectedmonth, int selectedday) {
						// TODO Auto-generated method stub
						selectedmonth = selectedmonth + 1;
						eventDateString = selectedday + "/" + selectedmonth
								+ "/" + selectedyear;
						eventDate.setText(eventDateString);
					}
				}, mYear, mMonth, mDay);
		mDatePicker.setTitle(R.string.insert_date);
		mDatePicker.show();
	}

	public void showEditText(View v) {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		// Set an EditText view to get user input
		final EditText input = new EditText(this);
		alert.setView(input);

		final EditText output = (EditText) findViewById(v.getId());

		if (output.getId() == eventVenue.getId()
				|| output.getId() == eventAddressStreet.getId()
				|| output.getId() == eventAddressCity.getId()
				|| output.getId() == eventBand.getId()) {
			input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
		} else if (output.getId() == eventDescription.getId()) {
			input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
					| InputType.TYPE_TEXT_FLAG_MULTI_LINE);
			input.setImeOptions(EditorInfo.IME_NULL);
			input.setVerticalScrollBarEnabled(true);
			input.setHorizontallyScrolling(false);
			input.setGravity(Gravity.TOP | Gravity.LEFT);
			input.setLines(5);
			input.setMaxLines(10);
		} else if (output.getId() == eventAddressDistrict.getId()) {
			input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
		}

		alert.setTitle(output.getHint());
		input.setText(output.getText());
		alert.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						String value = input.getText().toString();
						output.setText(value);
					}
				});

		alert.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// Canceled.
					}
				});
		input.requestFocus();
		AlertDialog alertd = alert.create();
		alertd.getWindow().setSoftInputMode(
				LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		alertd.show();
	}

	@Override
	protected void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this);
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this);
	}

}
